using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DABL;


public partial class Questionaire : System.Web.UI.Page
{
    private int _CurrentPage = 0;
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            if(Request.QueryString["ID"] != null)
           {
               string QuestionnaireID = Convert.ToString(Request.QueryString["ID"]);
               litQuestionnaire.Text = QuestionnaireController.QuestionnairesGetByID(QuestionnaireID).Tables[0].Rows[0]["QuestionnaireName"].ToString().Replace(Environment.NewLine, "<br>");
               LoadRepeater();
           }
        }

    }

    public int CurrentPage
    {
        get
        {
            // look for current page in ViewState
            object o = this.ViewState["_CurrentPage"];
            if(o == null)
                return 0;   // default to showing the first page
            else
                return (int)o;
        }

        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }
    

    public void LoadRepeater()
    {
        if(Request.QueryString["ID"] != null)
        {
            string QuestionnaireID = Convert.ToString(Request.QueryString["ID"]);

            PagedDataSource pdsRSS = new PagedDataSource();
            DataView dv = new DataView(QuestionnaireController.QuestionsGetByQuestionnaireID(QuestionnaireID).Tables[0]);
            pdsRSS.DataSource = dv;

            pdsRSS.AllowPaging = true;





            //if(CurrentPage == 0)
            //{
            //    pdsRSS.PageSize = Convert.ToInt16(ConfigurationManager.AppSettings["FirstPageQuestion"]);

            //}
            //else
            //{
            //    pdsRSS.PageSize = Convert.ToInt16(ConfigurationManager.AppSettings["OtherPagesQuestion"]);

            //}

            pdsRSS.PageSize = 5;
            // Build repeter for news articles

            //pdsRSS.PageSize = 5;
            if(CurrentPage == pdsRSS.PageCount-1 || pdsRSS.PageCount==1)
            {
                    btnSubmit.Text =  "Finish";
            }

             
            pdsRSS.CurrentPageIndex = CurrentPage;
            rptQuestions.DataSource = pdsRSS;
            rptQuestions.DataBind();

             
        }

    }

    private RadioButtonList getRadioButtonList(Int32 QuestionId)
    {
        RadioButtonList rdbList = new RadioButtonList();
        rdbList.ID ="rdb";
        rdbList.EnableViewState = true;

        
        
       
        rdbList.RepeatDirection = RepeatDirection.Horizontal;
        rdbList.RepeatLayout = RepeatLayout.Flow;

        ListItem listitem = new ListItem("1", "1");
        
        
        rdbList.Items.Add(listitem);
        listitem = new ListItem("2", "2");
        rdbList.Items.Add(listitem);
        listitem = new ListItem("3", "3");
        rdbList.Items.Add(listitem);
        listitem = new ListItem("4", "4");
        rdbList.Items.Add(listitem);
        listitem = new ListItem("5", "5");
        rdbList.Items.Add(listitem);

        return rdbList;
        

    }
    protected void rptQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Int32 QuestionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QuestionID"));
            Int32 QuestionType_FK = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QuestionType_FK"));
            
            //PlaceHolder plhOptions = (PlaceHolder)e.Item.FindControl("plhOptions");
             
            if(QuestionType_FK == 1)
            {
                TextBox txtBox = (TextBox)e.Item.FindControl("txtBox");
                txtBox.Visible = true;
            }
            else  if(QuestionType_FK == 2)
            {

                RadioButtonList rdbList = (RadioButtonList)e.Item.FindControl("rdbList");
                rdbList.RepeatDirection = RepeatDirection.Horizontal;
                rdbList.Visible = true;
                string UpperText = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "UpperText"));
                string LowerText = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "LowerText"));
                Literal litUpperText = (Literal)e.Item.FindControl("litUpperText");
                Literal litLowerText = (Literal)e.Item.FindControl("litLowerText");
                litUpperText.Visible = true;
                litLowerText.Visible = true;
                litUpperText.Text = UpperText;
                litLowerText.Text = LowerText;
                
            }
            else if(QuestionType_FK == 3)
            {
                CheckBoxList chkbList = (CheckBoxList)e.Item.FindControl("chkbList");
                chkbList.Visible = true;
                chkbList.DataTextField = "OptionText";
                chkbList.DataValueField = "OptionID";
                chkbList.DataSource = QuestionnaireController.OptionsGetByQuestionID(QuestionID);
                chkbList.DataBind();

            }
            else if(QuestionType_FK == 4)
            {
                RadioButtonList rdbList = (RadioButtonList)e.Item.FindControl("rdbList");
                rdbList.Visible = true;
                rdbList.DataTextField = "OptionText";
                rdbList.DataValueField = "OptionID";
                rdbList.RepeatDirection = RepeatDirection.Vertical;
                rdbList.DataSource = QuestionnaireController.OptionsGetByQuestionID(QuestionID);
                rdbList.DataBind();

            }
            

        }
      

       

    }
    protected void rptQuestions_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
    }
    protected void rptQuestions_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
       
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       
       
            foreach(RepeaterItem rptItem in rptQuestions.Items)
            {
                if(rptItem.ItemType == ListItemType.Item || rptItem.ItemType == ListItemType.AlternatingItem)
                {
                    HiddenField hdfQuestionID = (HiddenField)rptItem.FindControl("hdfQuestionID");
                    HiddenField hdfQuestionType_FK = (HiddenField)rptItem.FindControl("hdfQuestionType_FK");
                    Questionnaire dsQuestionnaire = new Questionnaire();
                    Questionnaire.QST_AnswersRow AnswersRow = (Questionnaire.QST_AnswersRow)dsQuestionnaire.QST_Answers.NewRow();
                    AnswersRow.QuestionID_FK = Convert.ToInt32(hdfQuestionID.Value);
                    AnswersRow.OptionID_FK = 0;
                    AnswersRow.Answer = "";

                    /*****Open Ended Quesiton ***************/

                    if(hdfQuestionType_FK.Value == "1")
                    {
                        TextBox txtBox = (TextBox)rptItem.FindControl("txtBox");
                        if(txtBox.Text != "")
                        {
                            AnswersRow.Answer = txtBox.Text;
                            QuestionnaireController.AnswersAdd(AnswersRow);
                        }

                    }/*****Likert Scale***************/

                    else if(hdfQuestionType_FK.Value == "2")
                    {
                        RadioButtonList rdbList = (RadioButtonList)rptItem.FindControl("rdbList");
                        AnswersRow.Answer = rdbList.SelectedValue;
                        QuestionnaireController.AnswersAdd(AnswersRow);

                    }/*****MCQ***************/

                    else if(hdfQuestionType_FK.Value == "3")
                    {
                        CheckBoxList chkbList = (CheckBoxList)rptItem.FindControl("chkbList");

                        foreach(ListItem listItem in chkbList.Items)
                        {
                            if(listItem.Selected)
                            {
                                AnswersRow.Answer = listItem.Text;
                                AnswersRow.OptionID_FK = Convert.ToInt32(listItem.Value);
                                QuestionnaireController.AnswersAdd(AnswersRow);
                            }

                        }

                    }
                    else if(hdfQuestionType_FK.Value == "4")
                    {
                        RadioButtonList rdbList = (RadioButtonList)rptItem.FindControl("rdbList");
                        AnswersRow.Answer = rdbList.SelectedValue;
                        QuestionnaireController.AnswersAdd(AnswersRow);
                       

                    }
                }

            }//for each

            if(btnSubmit.Text == "Next")
            {
                CurrentPage++;
                LoadRepeater();
            }
            else
            {
                litQuestionnaire.Text = "End";
                rptQuestions.Visible = false;
                btnSubmit.Visible = false;

            }

           

    }
}
